/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.services;

import com.google.inject.Inject;
import com.google.inject.Provider;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.impl.DirectSchedulerFactory;
import org.quartz.spi.JobFactory;
import org.xptracker.domain.Domain;
import org.xptracker.domain.ScheduledTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class JobScheduler implements ServletContextListener {
  private static Scheduler scheduler;
  private static Provider<Domain> domainProvider;
  private static JobFactory jobFactory;

  @Inject
  public void setDomain(Provider<Domain> domainProvider) {
    JobScheduler.domainProvider = domainProvider;
  }

  @Inject
  public void setJobFactory(JobFactory jobFactory) {
    JobScheduler.jobFactory = jobFactory;
  }

  public void contextInitialized(ServletContextEvent servletContextEvent) {
    start();
  }

  public void contextDestroyed(ServletContextEvent servletContextEvent) {
    stop();
  }

  public static void start() {
    try {
      stop();
      DirectSchedulerFactory.getInstance().createVolatileScheduler(1);
      scheduler = DirectSchedulerFactory.getInstance().getScheduler();
      scheduler.setJobFactory(jobFactory);
      scheduler.start();

      for (ScheduledTask task : domainProvider.get().listScheduledTasks()) {
        if (!task.isDisabled()) {
          Trigger t = new CronTrigger(task.getName(), null, task.getCronSchedule());
          JobDetail jd = new JobDetail(task.getName(), null, JsJob.class);
          jd.getJobDataMap().put(JsJob.JOB_ID, task.getId());
          LogFactory.getLog(JobScheduler.class).info("Registering " + task.toString());
          scheduler.scheduleJob(jd, t);
        } else {
          LogFactory.getLog(JobScheduler.class).warn("Task " + task.getName() + " is disabled.");
        }
      }
    } catch (Throwable t) {
      LogFactory.getLog(JobScheduler.class).error("Error starting scheduler.", t);
    }
  }

  public static void stop() {
    try {
      if (scheduler != null) {
        scheduler.shutdown();
      }
    } catch (Throwable t) {
      LogFactory.getLog(JobScheduler.class).error("Error starting scheduler.", t);
    }
  }
}
